package cn.mugutu.finance.controller;

import cn.mugutu.finance.annotation.SysResource;
import cn.mugutu.finance.domain.system.DictionaryType;
import cn.mugutu.finance.dto.BaseResult;
import cn.mugutu.finance.dto.EasyUIResult;
import cn.mugutu.finance.exception.BusinessException;
import cn.mugutu.finance.query.DictionaryTypeQuery;
import cn.mugutu.finance.service.system.DictionaryTypeService;
import cn.mugutu.finance.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/dictionaryType")
public class DictionaryTypeController extends BaseController {

    @Autowired
    private DictionaryTypeService dictionaryTypeService;

    @RequestMapping("/listUI")
    @SysResource(name = "字典类型管理", code = "dictionarytype:listui")
    public String listUI() {
        return "dictionaryType/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public EasyUIResult<DictionaryType> list(DictionaryTypeQuery query, int page, int rows) {
        setEasyUIParamsInQuery(query, page, rows);
        Pager<DictionaryType> pager = dictionaryTypeService.findByPage(query);
        return new EasyUIResult<>(pager.getTotalCount(), pager.getRows());
    }

    @RequestMapping("/all")
    @ResponseBody
    public List<DictionaryType> all() {
        return dictionaryTypeService.findAll();
    }
	
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public BaseResult saveOrUpdate(DictionaryType dictionaryType) {
        return dictionaryTypeService.saveOrUpdateDictionaryType(dictionaryType);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public BaseResult delete(Long id) {
        try {
            dictionaryTypeService.delete(id);
            return new BaseResult(true, "删除成功");
        } catch (Exception e) {
            throw new BusinessException("删除失败,请检查该字典类型是否已经存在了数据,或者联系系统管理员处理." + e.getMessage());
        }
    }

}
